﻿// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Collections.Generic;
using System.Text;

namespace SnapObjects.Data
{
    internal class BatchCommandBuilderResult
    {
        private readonly CommandBuilderContext _builderContext;
        private readonly List<UpdatableInfo> _updatableInfos;

        public BatchCommandBuilderResult(CommandBuilderContext builderContext)
        {
            _builderContext = builderContext;

            _updatableInfos = new List<UpdatableInfo>();
        }

        public string BuiltSql => _builderContext.SqlBuilder.ToString();

        public IReadOnlyList<IAdoDbParameter> AdoDbParameters
        {
            get
            {
                return _builderContext.AdoDbParameters; 
            }
        }

        public IReadOnlyList<ResultSetMapping> ResultSetMappings => _builderContext.ResultSetMappings;

        public IReadOnlyList<UpdatableInfo> Updatables => _updatableInfos;

        public void AddUpdatable(
            IUpdatable updatable, ModelUpdateInfo modelUpdateInfo, ResultSetMapping resultSetMapping)
        {
            var info = new UpdatableInfo(updatable, resultSetMapping, modelUpdateInfo);

            _updatableInfos.Add(info);
        }
    }

    internal class UpdatableInfo
    {
        public UpdatableInfo(
            IUpdatable updatable, ResultSetMapping resultSetMapping, ModelUpdateInfo modelUpdateInfo)
        {
            this.Updatable = updatable;
            this.ResultSetMapping = resultSetMapping;
            this.ModelUpdateInfo = modelUpdateInfo;
        }

        public IUpdatable Updatable { get; }
        public ResultSetMapping ResultSetMapping { get; }
        public ModelUpdateInfo ModelUpdateInfo { get; }
    }
}
